Add Mistral Small 4 (119B MoE) support via mistral4.py#1037
Open
ProducerGuy wants to merge 1 commit intoml-explore:mainfrom
Open
Add Mistral Small 4 (119B MoE) support via mistral4.py#1037ProducerGuy wants to merge 1 commit intoml-explore:mainfrom
ProducerGuy wants to merge 1 commit intoml-explore:mainfrom
Conversation
Adds MoE + MLA model support for Mistral Small 4 (mistralai/Mistral-Small-4-119B-2603), enabling mlx-community/Mistral-Small-4-119B-2603-4bit to load and run. New file: mlx_lm/models/mistral4.py - MoE feedforward with SwitchGLU routing (128 experts, top-4) - Shared expert support - MLA attention with compressed KV via explicit kv_b_proj (distinct from DeepSeek V3's MultiLinear approach) - Standard attention fallback for dense layers - All dimensions read from config, nothing hardcoded Modified: mlx_lm/models/mistral3.py - Structural routing: n_routed_experts presence routes to mistral4 - Forward-compatible with future MoE Mistral variants - Dense Ministral 3B/8B/14B models unaffected Tested on MacBook Pro M5 Max (128GB): - 104 tok/s generation - 67 GB peak memory - Correct factual output confirmed Before: ValueError: Received 1260 parameters not in model After: Model loads and generates correctly Chat template (apply_chat_template) works out of the box once the model loads — no additional changes needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for Mistral Small 4 (
mistralai/Mistral-Small-4-119B-2603), a 119B-parameter Mixture-of-Experts model with 128 experts and 4 active per token (6B active parameters).Enables
mlx-community/Mistral-Small-4-119B-2603-4bitto load and run.Before
After
Changes
New file:
mlx_lm/models/mistral4.pySwitchGLUrouting (128 experts, top-4 selection)kv_b_projlinear layer for KV decompression — this is architecturally distinct from DeepSeek V3'sMultiLinearapproach; Mistral Small 4 uses a single linear projection rather than per-head Kronecker-style decompositionkv_lora_rank,q_lora_rank,qk_rope_head_dim,v_head_dim, etc.) read fromconfig.json, nothing hardcodedModified:
mlx_lm/models/mistral3.py(9 lines added, 2 removed)mistral4.Modelwhenn_routed_expertsis present intext_configNotes
apply_chat_templateworks out of the box once the model loads — the raw prompt test output (JSON-formatted) is expected without the chat template and is not a bugreasoning_effortparameter support is intentionally left for a follow-up — this PR focuses on correct inference onlyTest plan